package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.MovementService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementService movementService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity postMovement(MultipartFile[] imageContent, @RequestParam Map<String, String> params) {
        movementService.postMovement(imageContent, params);
        return ResponseEntity.ok(null);
    }

    /**
     * 获取好友动态
     */
    @GetMapping
    public ResponseEntity friendMovement(PageResult pageResult) {
        PageResult result = movementService.friendMovement(pageResult);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取推荐动态
     *
     * @param pageResult
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(PageResult pageResult) {
        PageResult result = movementService.getRecommend(pageResult);

        return ResponseEntity.ok(result);
    }

    /**
     * 喜欢动态
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity loveMovement(@PathVariable ObjectId id) {
        Integer integer = movementService.loveMovement(id);
        return ResponseEntity.ok(integer);
    }

    /**
     * 取消喜欢
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unloveMovement(@PathVariable ObjectId id) {
        Integer integer = movementService.unloveMovement(id);
        return ResponseEntity.ok(integer);

    }

    /**
     * 点赞动态
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity likeMovement(@PathVariable ObjectId id) {
        Integer integer = movementService.likeMovement(id);
        return ResponseEntity.ok(integer);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislikeMovement(@PathVariable ObjectId id) {
        Integer integer = movementService.dislikeMovement(id);
        return ResponseEntity.ok(integer);
    }

    /**
     * 获取用户动态
     *
     * @param pageResult
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity allMovement(PageResult pageResult, @RequestParam Long userId) {
        PageResult<MovementVo> result = movementService.allMovement(pageResult, userId);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取单个动态
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity movementComment(@PathVariable ObjectId id) {
        MovementVo vo = movementService.getMovement(id);
        return ResponseEntity.ok(vo);
    }

    /**
     *      获取谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity Visitors(){
        List<TodayBest> voList = movementService.findLast4();
        return ResponseEntity.ok(voList);
    }


}
